using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace eTMS.Commons
{
    [Serializable]
    public class Configs
    {

        [NonSerialized]
        private const string ConfigFile = "Configs.ini";
        private static Configs _configs = null;
        private static string GetFilePath(string ConfigFile)
        {
            string st = System.Reflection.Assembly.GetCallingAssembly().Location;
            st = System.IO.Path.GetDirectoryName(st).TrimEnd('\\') + "\\Configs";
            if (!System.IO.Directory.Exists(st))
                System.IO.Directory.CreateDirectory(st);
            return System.IO.Path.Combine(st, ConfigFile);

        }
        public string GetConfigs(string key)
        {
            if(Parameters!=null)
            {
                if(Parameters.ContainsKey(key))
                {
                    return Parameters[key].Trim();
                }
            }
            return "";
        }
        public  static  Configs OConfigs
        {
            get
            {
                return _configs;
            }
        }
        
        public static Configs Load()
        {
            if (_configs == null)
            {
                _configs = Load(ConfigFile);
            }
            if (_configs == null)
                _configs = new Configs();
            return _configs;
        }
        public static Configs Load(string ConfigFile)
        {
            Configs o = new Configs();
            try
            {
                if (System.IO.File.Exists(GetFilePath(ConfigFile)))
                {
                    System.IO.FileStream strm = System.IO.File.Open(GetFilePath(ConfigFile), System.IO.FileMode.OpenOrCreate);
                    try
                    {
                        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter ftm = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                        o = (Configs)ftm.Deserialize(strm);
                    }
                    catch
                    {
                    }
                    strm.Close();

                }
            }
            catch
            {
            }
            return o;

        }

        public Dictionary<string, string> Parameters = new Dictionary<string, string>();
        public static void Save()
        {
            Save(ConfigFile, _configs);
        }
        public static void Save(string ConfigFile, Configs configs)
        {
            try
            {
                System.IO.FileStream strm = System.IO.File.Create(GetFilePath(ConfigFile));
                try
                {
                    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter ftm = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    ftm.Serialize(strm, configs);
                }
                catch
                {
                }
                strm.Close();
            }
            catch
            {
            }

        }
        public void AddParameter(string ParamName, string Value)
        {
            if (!Parameters.ContainsKey(ParamName))
            {
                Parameters.Add(ParamName, Value);
            }
            else
            {
                Parameters[ParamName] = Value;
            }
        }
    }
}
